package org.fao.pims.service.pollinteractions;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fao.pims.model.*;
import org.fao.pims.persistence.*;


public class SpeciesInteractionsManagerImpl implements SpeciesInteractionsManager {

	private static Log log = LogFactory.getLog(SpeciesInteractionsManagerImpl.class);
	
	public SPModel getSpeciesModel(String name) throws SpeciesInteractionsException {
		
		SPModel model = null;
		
		InfoItem item = null;
		
		Taxon taxon = null;
		
		PollinatorSpecie species = null;
		
		TaxonDAO taxonDao = null;
		
		InfoItemDao itemDao = null;
		
		try {
			
			taxonDao = new TaxonDAOImpl();
		
			taxon = taxonDao.getTaxonByScientificName(name);
			
			itemDao = new InfoItemDaoImpl();
			
			item = itemDao.getInfoItemByTaxonId(taxon.getId());
			
			if(item != null){
			
				species = new PollinatorSpecie();
			
				species.setScientificName(name);
			
				model = new SPModel();
			
				model.setPollinator(species);
			
				model.setInfoItem(item);
			}
			else {
				//data not found				
				throw new SpeciesInteractionsException();
			}
		
		}
		catch(DataAccessException dae) {
			
			log.error(dae.getMessage());
			
			throw new SpeciesInteractionsException();
			
		}
		
		return model;
		
	}

}
